package org.jeecg.api;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/user")
public class UploadController {
    @Value("${uploadpath}")
    public String path;

    @PostMapping("/upload")
    @ResponseBody
    public Map upload(@RequestParam("file") MultipartFile file) {
        Map map = new HashMap<>();
        if (file.isEmpty()) {
            map.put("code","0001");
            ((HashMap) map).put("message","上传失败，请选择文件");
            return map;
        }

        String fileName = file.getOriginalFilename();
        String newFileName = System.currentTimeMillis()+"."+fileName.split("\\.")[1];

        File dest = new File(path +newFileName);
        try {

            file.transferTo(dest);
            map.put("path",newFileName);
            map.put("code","0000");
            ((HashMap) map).put("message","上传成功");
            return  map;
        } catch (IOException e) {

        }
        map.put("code","0001");
        ((HashMap) map).put("message","上传失败，请选择文件");
        return map;
    }
}
